import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'
import AdminDashboard from '@/views/admin/Dashboard.vue'
import StationDashboard from '@/views/station/Dashboard.vue'
import CustomerDashboard from '@/views/customer/Dashboard.vue'

// 管理员页面
import UserManagement from '@/views/admin/UserManagement.vue'
import ItemManagement from '@/views/admin/ItemManagement.vue'
import StationManagement from '@/views/admin/StationManagement.vue'
import SystemSettings from '@/views/admin/SystemSettings.vue'
import DataAnalysis from '@/views/admin/DataAnalysis.vue'

// 站点管理员页面
import OrderProcessing from '@/views/station/OrderProcessing.vue'
import InventoryManagement from '@/views/station/InventoryManagement.vue'
import StationInfo from '@/views/station/StationInfo.vue'

// 客户页面
import ItemBrowse from '@/views/customer/ItemBrowse.vue'
import CreateOrder from '@/views/customer/CreateOrder.vue'
import OrderTracking from '@/views/customer/OrderTracking.vue'
import PersonalInfo from '@/views/customer/PersonalInfo.vue'
import OrderHistory from '@/views/customer/OrderHistory.vue'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  // 管理员路由
  {
    path: '/admin',
    name: 'AdminLayout',
    component: () => import('@/layouts/AdminLayout.vue'),
    meta: { requiresAuth: true, role: 'admin' },
    children: [
      {
        path: '',
        name: 'AdminRedirect',
        redirect: '/admin/dashboard'
      },
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: AdminDashboard
      },
      {
        path: 'users',
        name: 'UserManagement',
        component: UserManagement
      },
      {
        path: 'items',
        name: 'ItemManagement',
        component: ItemManagement
      },
      {
        path: 'stations',
        name: 'StationManagement',
        component: StationManagement
      },
      {
        path: 'settings',
        name: 'SystemSettings',
        component: SystemSettings
      },
      {
        path: 'analysis',
        name: 'DataAnalysis',
        component: DataAnalysis
      }
    ]
  },
  // 站点管理员路由
  {
    path: '/station',
    name: 'StationLayout',
    component: () => import('@/layouts/StationLayout.vue'),
    meta: { requiresAuth: true, role: 'station_manager' },
    children: [
      {
        path: '',
        name: 'StationRedirect',
        redirect: '/station/dashboard'
      },
      {
        path: 'dashboard',
        name: 'StationDashboard',
        component: StationDashboard
      },
      {
        path: 'orders',
        name: 'OrderProcessing',
        component: OrderProcessing
      },
      {
        path: 'inventory',
        name: 'InventoryManagement',
        component: InventoryManagement
      },
      {
        path: 'info',
        name: 'StationInfo',
        component: StationInfo
      }
    ]
  },
  // 客户路由
  {
    path: '/customer',
    name: 'CustomerLayout',
    component: () => import('@/layouts/CustomerLayout.vue'),
    meta: { requiresAuth: true, role: 'customer' },
    children: [
      {
        path: '',
        name: 'CustomerRedirect',
        redirect: '/customer/dashboard'
      },
      {
        path: 'dashboard',
        name: 'CustomerDashboard',
        component: CustomerDashboard
      },
      {
        path: 'browse',
        name: 'ItemBrowse',
        component: ItemBrowse
      },
      {
        path: 'create-order',
        name: 'CreateOrder',
        component: CreateOrder
      },
      {
        path: 'orders',
        name: 'OrderTracking',
        component: OrderTracking
      },
      {
        path: 'history',
        name: 'OrderHistory',
        component: OrderHistory
      },
      {
        path: 'profile',
        name: 'PersonalInfo',
        component: PersonalInfo
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token')
  const userRole = localStorage.getItem('userRole')
  
  // 如果是登录或注册页面，直接通过
  if (to.path === '/login' || to.path === '/register') {
    next()
    return
  }
  
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!token) {
      next('/login')
    } else if (to.meta.role && to.meta.role !== userRole) {
      // 角色不匹配，重定向到对应角色的首页
      switch (userRole) {
        case 'admin':
          next('/admin')
          break
        case 'station_manager':
          next('/station')
          break
        case 'customer':
          next('/customer')
          break
        default:
          next('/login')
      }
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
